package com.ws.game.lesson6
{
	import com.ws.game.common.framework.BaseCtrl;
	import com.ws.game.common.framework.BaseGameObject;
	import com.ws.game.common.global.Global;
	import com.ws.game.common.utils.MoveDirectionType;
	import com.ws.game.lesson3.fire.Player;
	
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	import mx.utils.StringUtil;
	
	public class Action8Object extends Player
	{
		public function Action8Object(aCtrl:BaseCtrl,aFace:Sprite)
		{
			super(aCtrl,aFace);
		}
		
		override protected function move():void{
//			super.move();
			if (!nextCanMove) {
				return; 
			}
			switch(walkDirection)
			{
				case MoveDirectionType.UP:
					y -= speed;
					break;
				case MoveDirectionType.DOWN:
					y += speed;
					break;
				case MoveDirectionType.LEFT:
					x -= speed;
					break;
				case MoveDirectionType.RIGHT:
					x += speed;
					break;
				case MoveDirectionType.RIGHT_DOWN:
					x += speed;
					y += speed;
					break;
				case MoveDirectionType.RIGHT_UP:
					x += speed;
					y -= speed;
					break;
				case MoveDirectionType.LEFT_DOWN:
					x -= speed;
					y += speed;
					break;
				case MoveDirectionType.LEFT_UP:
					x -= speed;
					y -= speed;
					break;
				default:
					break;
			}
		}
		
		override public function set direction(value:uint):void{
//			trace(StringUtil.substitute("direction:{0}",value));
			if(direction!=value)
			{
				if(_face ){
					var angle:Number;
					var tx:Number;
					var ty:Number;			
					switch(value){
						case MoveDirectionType.UP:
							angle = 0;
							tx = 0;
							ty = 0;
							break;
						case MoveDirectionType.RIGHT:
							angle = Math.PI * 0.5;
							tx = walkDirection == MoveDirectionType.RIGHT_DOWN || walkDirection == MoveDirectionType.RIGHT_UP? Math.sqrt( Math.pow(_face.width,2)/2):_face.width;
							ty = 0;
							break;
						case MoveDirectionType.DOWN:
							angle = Math.PI;
							tx = walkDirection == MoveDirectionType.RIGHT_DOWN || walkDirection == MoveDirectionType.LEFT_DOWN? Math.sqrt( Math.pow(_face.width,2)/2) : _face.width;
							ty = walkDirection == MoveDirectionType.RIGHT_DOWN || walkDirection == MoveDirectionType.LEFT_DOWN? Math.sqrt( Math.pow(_face.width,2)/2) :_face.height;
							break;
						case MoveDirectionType.LEFT:
							angle = Math.PI * 1.5;
							tx = 0;
							ty =  walkDirection == MoveDirectionType.LEFT_DOWN || walkDirection == MoveDirectionType.LEFT_UP? Math.sqrt( Math.pow(_face.width,2)/2): _face.height;
							break;
						case MoveDirectionType.LEFT_UP:
							angle = Math.PI * 1.75;
							tx = (_face.width- Math.sqrt(Math.pow(_face.width,2) +Math.pow(_face.height,2)))/2;
							ty = _face.height/2;
							break;
						case MoveDirectionType.LEFT_DOWN:
							angle = Math.PI * 1.25;
							tx = _face.width/2;
							ty = (Math.sqrt(Math.pow(_face.width,2) +Math.pow(_face.height,2))+_face.height)/2;
							break;
						case MoveDirectionType.RIGHT_UP:
							angle = Math.PI * 0.25;
							tx = _face.width/2;
							ty = (_face.height-Math.sqrt(Math.pow(_face.width,2) +Math.pow(_face.height,2)))/2;
							break;
						case MoveDirectionType.RIGHT_DOWN:
							angle = Math.PI * 0.75;
							tx = (Math.sqrt(Math.pow(_face.width,2) +Math.pow(_face.height,2))+_face.width)/2 ;
							ty = _face.height/2;
							break;
						default:
							break;
					}
					if(!isNaN(angle) && !isNaN(tx) &&!isNaN(ty)){
						_face.transform.matrix = new Matrix(Math.cos(angle), Math.sin(angle),Math.sin(angle)*-1,Math.cos(angle),tx,ty);
					}
					
					walkDirection = value;
				}
			}
		}
		
		override public function get nextCanMove():Boolean{
			var nx:uint = x;
			var ny:uint = y;
			// 根据移动方向进行处理，计算出下一目标点位置
			switch(walkDirection)
			{
				case MoveDirectionType.UP:
					ny = y-speed;
					break;
				case MoveDirectionType.DOWN:
					ny = y+speed;
					break;
				case MoveDirectionType.LEFT:
					nx = x-speed;
					break;
				case MoveDirectionType.RIGHT:
					nx = x+speed;
					break;
				case MoveDirectionType.RIGHT_DOWN:
					nx = x+speed;
					ny = y+speed;
					break;
				case MoveDirectionType.RIGHT_UP:
					nx = x+speed;
					ny = y-speed;
					break;
				case MoveDirectionType.LEFT_DOWN:
					nx = x-speed;
					ny = y+speed;
					break;
				case MoveDirectionType.LEFT_UP:
					nx = x-speed;
					ny = y-speed;
					break;
				default:break;
			}
			
			// 如果下一目标点超出屏幕范围，则不能移动
			if (nx > Global.stage.stageWidth - width || nx < 0) 
				return false;
			if (ny > Global.stage.stageHeight - height || ny < 0)
				return false;
			
			for each(var obj:BaseGameObject in Global.scene.gObjects)
			{
				if (obj == this || !obj.hitTest){
					continue;
				}
				for each(var p:Array in _hitPoint){
					if (obj.hitTestPoint(nx + p[0], ny + p[1], true)){		
						return false;
					}
				}
			}
			
			// 检测通过
			return true;
		}
		
		private function get isNormalDirection():Boolean{
			return (this.walkDirection <= MoveDirectionType.RIGHT && this.walkDirection >MoveDirectionType.STOP);		
		}
	}
}